package com.tanhua.recommend.msg;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;


@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-consumer")
public class QuanZiMsgConsumer implements RocketMQListener<String> {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String msg) {
        try {
            JsonNode jsonNode = MAPPER.readTree(msg);
            int type = jsonNode.get("type").asInt();
            String publishId = jsonNode.get("publishId").asText();
            long date = jsonNode.get("date").asLong();
            long userId = jsonNode.get("userId").asLong();
            long pid = jsonNode.get("pid").asLong();

            RecommendQuanZi recommendQuanZi = new RecommendQuanZi(ObjectId.get(), userId, pid, null, date);

            switch (type) {
                case 1:
                    int score = 0;
                    Publish publish = mongoTemplate.findById(new ObjectId(publishId), Publish.class);
                    if (publish != null) {
                        int length = StringUtils.length(publish.getText());
                        if (length <= 50) {
                            score = 1;
                        } else if (length <= 100) {
                            score = 2;
                        } else {
                            score = 3;
                        }
                        if (CollectionUtils.isNotEmpty(publish.getMedias())) {
                            score = score + publish.getMedias().size();
                        }
                        recommendQuanZi.setScore((double) score);
                    } else {
                        recommendQuanZi.setScore(0d);
                    }
                    break;
                case 2:
                    recommendQuanZi.setScore(1d);
                    break;
                case 3:
                    recommendQuanZi.setScore(5d);
                    break;
                case 4:
                    recommendQuanZi.setScore(8d);
                    break;
                case 5:
                    recommendQuanZi.setScore(10d);
                    break;
                case 6:
                    recommendQuanZi.setScore(-5d);
                    break;
                case 7:
                    recommendQuanZi.setScore(-8d);
                    break;
                default:
                    recommendQuanZi.setScore(0d);
                    break;
            }

            String collectName="recommend_quanzi_"+new DateTime().toString("yyyyMMdd");
            mongoTemplate.save(recommendQuanZi,collectName);
        } catch (Exception e) {
            log.error("消息处理失败! msg=" + msg, e);
        }

    }
}
